﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EnglishAuction.BUS
{
    public class NguoiDungBUS
    {
        
        public static List<NguoiDung> GetAll()
        {
            using (var context = new EnglishAuctionEntities())
            {
                return context.NguoiDungs.ToList();
            }
        }

        public static bool CheckCMND(string cmnd, int id = -1)
        {
            bool result = false;

            using (var context = new EnglishAuctionEntities())
            {
                var lst = context.NguoiDungs.ToList();
                if (id != -1)
                {
                    lst = context.NguoiDungs.Where(p => p.Id != id).ToList();
                }
                var tmp = lst.Where(p => p.CMND.Trim().Equals(cmnd));
                
                result = (tmp != null && tmp.Count() > 0) ? true : false;
            }

            return result;
        }

        public static bool CheckEmail(string email, int id = -1)
        {
            bool result = false;

            using (var context = new EnglishAuctionEntities())
            {
                var lst = context.NguoiDungs.ToList();
                if (id != -1)
                {
                    lst = context.NguoiDungs.Where(p => p.Id != id).ToList();
                }
                var tmp = lst.Where(p => p.Email.Trim().Equals(email));
                result = (tmp != null && tmp.Count() > 0) ? true : false;
            }

            return result;
        }

        public static bool CheckUserName(string username)
        {
            bool result = false;

            using (var context = new EnglishAuctionEntities())
            {
                var tmp = context.NguoiDungs.Where(p => p.TaiKhoan.Trim().Equals(username));
                result = (tmp != null && tmp.Count() > 0) ? true : false;
            }

            return result;
        }

        public static bool CheckPassword(string pass, int id)
        {
            bool result = false;
            using (var context = new EnglishAuctionEntities())
            {
               var tmp = context.NguoiDungs.Where(p => p.Id == id  && p.MatKhau.Trim().Equals(pass));
                result = (tmp != null && tmp.Count() > 0) ? true : false;
            }
            return result;
        }
        public static NguoiDung Login(string username, string password)
        {
            NguoiDung nguoiDung = null;
            using (var context = new EnglishAuctionEntities())
            {
                var result = context.NguoiDungs.Where(p => p.TaiKhoan.Trim().Equals(username) &&
                    p.MatKhau.Trim().Equals(password));
                if (result != null && result.Count() > 0)
                {
                    nguoiDung = result.FirstOrDefault();
                }
            }

            return nguoiDung;
        }

        public static bool AddNguoiDung(NguoiDung nguoiDung)
        {
            bool result = true;
            try
            {
                using (var context = new EnglishAuctionEntities())
                {
                    context.NguoiDungs.Add(nguoiDung);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                result = false;
            }

            return result;
        }

        public static NguoiDung GetNguoiDungBySanPhamId(int sanPhamId)
        {
            using (var context = new EnglishAuctionEntities())
                return context.SanPhams.Where(p => p.Id == sanPhamId).FirstOrDefault().NguoiDung;
        }

        public static NguoiDung GetById(int id)
        {
            using (var context = new EnglishAuctionEntities())
                return context.NguoiDungs.Find(id);
        }

        public static void Update(NguoiDung nguoiDung)
        {
            using (var db = new EnglishAuctionEntities())
            {
                var existing = db.NguoiDungs.Find(nguoiDung.Id);
                existing.CMND = nguoiDung.CMND;
                existing.DiaChi = nguoiDung.DiaChi;
                existing.Email = nguoiDung.Email;
                existing.GioiTinh = nguoiDung.GioiTinh;
                existing.HoTen = nguoiDung.HoTen;
                existing.LoaiNguoiDungId = nguoiDung.LoaiNguoiDungId;
                existing.MatKhau = nguoiDung.MatKhau;
                existing.NgaySinh = nguoiDung.NgaySinh;
                existing.SoDienThoai = nguoiDung.SoDienThoai;
                existing.TaiKhoan = nguoiDung.TaiKhoan;
                db.SaveChanges();
            }
        }

        public static bool UpdatePassword(NguoiDung nguoiDung)
        {
            using (var db = new EnglishAuctionEntities())
            {
                var existing = db.NguoiDungs.Find(nguoiDung.Id);
                existing.MatKhau = nguoiDung.MatKhau;
                int value=db.SaveChanges();
                if (value > 0)
                {
                    return true;
                }
                else
                    return false;
            }
        }

        public static void ResetPassword(int id, string password)
        {
            using (var context = new EnglishAuctionEntities())
            {
                var user = context.NguoiDungs.Where(u => u.Id == id).FirstOrDefault();
                user.MatKhau = password;
                context.SaveChanges();
            }
        }
    }
}